import { NThing, type ThingProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import type { BaseComponentType } from "../types";
import { getSlotsDom } from "../utils";
export type ThingFactoryConstructorType = {
  attrs: HTMLAttributes;
  props: ThingProps;
  defaultSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  avatarSlot?: BaseComponentType;
  descriptionSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  headerExtraSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
};
export class ThingFactory {
  private attrs: HTMLAttributes = {};
  private props: ThingProps = {};
  private defaultSlot?: BaseComponentType = null;
  private actionSlot?: BaseComponentType = null;
  private avatarSlot?: BaseComponentType = null;
  private descriptionSlot?: BaseComponentType = null;
  private footerSlot?: BaseComponentType = null;
  private headerExtraSlot?: BaseComponentType = null;
  private headerSlot?: BaseComponentType = null;
  constructor(data?: ThingFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.props) this.setProps(data.props);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.actionSlot) this.setAction(data.actionSlot);
    if (data?.avatarSlot) this.setAvatar(data.avatarSlot);
    if (data?.descriptionSlot) this.setDescription(data.descriptionSlot);
    if (data?.footerSlot) this.setFooter(data.footerSlot);
    if (data?.headerExtraSlot) this.setHeaderExtra(data.headerExtraSlot);
    if (data?.headerSlot) this.setHeader(data.headerSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: ThingProps) {
    this.props = props;
    return this;
  }

  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }

  setAvatar(component: BaseComponentType) {
    this.avatarSlot = component;
    return this;
  }

  setDescription(component: BaseComponentType) {
    this.descriptionSlot = component;
    return this;
  }

  setFooter(component: BaseComponentType) {
    this.footerSlot = component;
    return this;
  }

  setHeaderExtra(component: BaseComponentType) {
    this.headerExtraSlot = component;
    return this;
  }

  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }

  create() {
    return (
      <NThing {...this.props} {...this.attrs}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot as BaseComponentType)
              : null,

          action:
            this.actionSlot != null
              ? () => getSlotsDom(this.actionSlot as BaseComponentType)
              : null,

          avatar:
            this.avatarSlot != null
              ? () => getSlotsDom(this.avatarSlot as BaseComponentType)
              : null,
          description:
            this.descriptionSlot != null
              ? () => getSlotsDom(this.descriptionSlot as BaseComponentType)
              : null,
          footer:
            this.footerSlot != null
              ? () => getSlotsDom(this.footerSlot as BaseComponentType)
              : null,
          "header-extra":
            this.headerExtraSlot != null
              ? () => getSlotsDom(this.headerExtraSlot as BaseComponentType)
              : null,
          header:
            this.headerSlot != null
              ? () => getSlotsDom(this.headerSlot as BaseComponentType)
              : null,
        }}
      </NThing>
    );
  }
}
